package splug.cjk;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

public class BBSCarPoolTab2Search extends Activity {

	private Button search;
	private Button back;
	private Spinner start1;
	private Spinner start2;
	private Spinner finish1;
	private Spinner finish2;
	private ArrayAdapter<String> adapter;
	private String id;
	private SearchOption option = new SearchOption();
	private ArrayList<Content> content = new ArrayList<Content>();
	private ArrayList<BBSCarPoolContent> sub_content = new ArrayList<BBSCarPoolContent>();
	private int cnt = 0;	//검색조건에 맞는 결과가 몇개나 있는지 기록하는 변수

	@SuppressWarnings("unchecked")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitle("카풀 게시판 검색하기");
		setContentView(R.layout.bbs_carpool_tab1_search);

		Bundle b = getIntent().getExtras();
		id = b.getString("ID");
		sub_content = (ArrayList<BBSCarPoolContent>) b.getSerializable("SUB");
		content = (ArrayList<Content>) b.getSerializable("TITLE");

		search = (Button) findViewById(R.id.btn_carpool_tab1_search_ok);
		back = (Button) findViewById(R.id.btn_carpool_tab1_search_cancel);
		start1 = (Spinner) findViewById(R.id.spin_tab1_search_1);
		start2 = (Spinner) findViewById(R.id.spin_tab1_search_2);
		finish1 = (Spinner) findViewById(R.id.spin_tab1_search_3);
		finish2 = (Spinner) findViewById(R.id.spin_tab1_search_4);

		adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, option.city);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);  
		start1.setAdapter(adapter);
		finish1.setAdapter(adapter);

		adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, option.nothing);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);  
		start2.setAdapter(adapter);
		finish2.setAdapter(adapter);

		start1.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				if(arg2 == 1){
					ArrayList<String> seoul = option.seoul;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab2Search.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					start2.setAdapter(adapter);
				}
				else if(arg2 == 2){
					ArrayList<String> seoul = option.busan;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab2Search.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					start2.setAdapter(adapter);
				}
				else if(arg2 == 3){
					ArrayList<String> seoul = option.daegu;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab2Search.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					start2.setAdapter(adapter);
				}
				else if(arg2 == 4){
					ArrayList<String> seoul = option.incheon;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab2Search.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					start2.setAdapter(adapter);
				}
				else if(arg2 == 5){
					ArrayList<String> seoul = option.gwangju;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab2Search.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					start2.setAdapter(adapter);
				}
				else if(arg2 == 6){
					ArrayList<String> seoul = option.daejun;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab2Search.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					start2.setAdapter(adapter);
				}
				else if(arg2 == 7){
					ArrayList<String> seoul = option.ulsan;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab2Search.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					start2.setAdapter(adapter);
				}
				else if(arg2 == 8){
					ArrayList<String> seoul = option.kyonggi;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab2Search.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					start2.setAdapter(adapter);
				}
				else if(arg2 == 9){
					ArrayList<String> seoul = option.kangwon;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab2Search.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					start2.setAdapter(adapter);
				}
				else if(arg2 == 10){
					ArrayList<String> seoul = option.chungbuk;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab2Search.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					start2.setAdapter(adapter);
				}
				else if(arg2 == 11){
					ArrayList<String> seoul = option.chungnam;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab2Search.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					start2.setAdapter(adapter);
				}
				else if(arg2 == 12){
					ArrayList<String> seoul = option.junbuk;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab2Search.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					start2.setAdapter(adapter);
				}
				else if(arg2 == 13){
					ArrayList<String> seoul = option.junnam;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab2Search.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					start2.setAdapter(adapter);
				}
				else if(arg2 == 14){
					ArrayList<String> seoul = option.kyongbuk;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab2Search.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					start2.setAdapter(adapter);
				}
				else if(arg2 == 15){
					ArrayList<String> seoul = option.kyongnam;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab2Search.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					start2.setAdapter(adapter);
				}
				else if(arg2 == 16){
					ArrayList<String> seoul = option.jeju;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab2Search.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					start2.setAdapter(adapter);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});

		finish1.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				if(arg2 == 1){
					ArrayList<String> seoul = option.seoul;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab2Search.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					finish2.setAdapter(adapter);
				}
				else if(arg2 == 2){
					ArrayList<String> seoul = option.busan;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab2Search.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					finish2.setAdapter(adapter);
				}
				else if(arg2 == 3){
					ArrayList<String> seoul = option.daegu;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab2Search.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					finish2.setAdapter(adapter);
				}
				else if(arg2 == 4){
					ArrayList<String> seoul = option.incheon;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab2Search.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					finish2.setAdapter(adapter);
				}
				else if(arg2 == 5){
					ArrayList<String> seoul = option.gwangju;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab2Search.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					finish2.setAdapter(adapter);
				}
				else if(arg2 == 6){
					ArrayList<String> seoul = option.daejun;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab2Search.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					finish2.setAdapter(adapter);
				}
				else if(arg2 == 7){
					ArrayList<String> seoul = option.ulsan;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab2Search.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					finish2.setAdapter(adapter);
				}
				else if(arg2 == 8){
					ArrayList<String> seoul = option.kyonggi;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab2Search.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					finish2.setAdapter(adapter);
				}
				else if(arg2 == 9){
					ArrayList<String> seoul = option.kangwon;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab2Search.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					finish2.setAdapter(adapter);
				}
				else if(arg2 == 10){
					ArrayList<String> seoul = option.chungbuk;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab2Search.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					finish2.setAdapter(adapter);
				}
				else if(arg2 == 11){
					ArrayList<String> seoul = option.chungnam;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab2Search.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					finish2.setAdapter(adapter);
				}
				else if(arg2 == 12){
					ArrayList<String> seoul = option.junbuk;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab2Search.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					finish2.setAdapter(adapter);
				}
				else if(arg2 == 13){
					ArrayList<String> seoul = option.junnam;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab2Search.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					finish2.setAdapter(adapter);
				}
				else if(arg2 == 14){
					ArrayList<String> seoul = option.kyongbuk;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab2Search.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					finish2.setAdapter(adapter);
				}
				else if(arg2 == 15){
					ArrayList<String> seoul = option.kyongnam;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab2Search.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					finish2.setAdapter(adapter);
				}
				else if(arg2 == 16){
					ArrayList<String> seoul = option.jeju;
					adapter = new ArrayAdapter<String>(BBSCarPoolTab2Search.this, android.R.layout.simple_spinner_item, seoul);
					adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
					finish2.setAdapter(adapter);
				}
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});

		search.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String start = start1.getSelectedItem().toString() + " "
				+ start2.getSelectedItem().toString();
				String finish = finish1.getSelectedItem().toString() + " "
				+ finish2.getSelectedItem().toString();

				if(start.indexOf("광역시/도") != -1 || finish.indexOf("광역시/도") != -1){
					/*
					 * 아무것도 선택되지 않았으면 예외처리
					 */
					Toast.makeText(BBSCarPoolTab2Search.this, "출발지 목적지를 선택해주세요", Toast.LENGTH_SHORT).show();
				}
				else{
					/*
					 * 출발지와 도착지가 일치하는 것이 있는지 검사한다.
					 */
					ArrayList<Content> result_title = new ArrayList<Content>();
					ArrayList<BBSCarPoolContent> result_sub = new ArrayList<BBSCarPoolContent>();
					for(int i = 0; i < sub_content.size(); i++){
						if(start.equals(sub_content.get(i).getStart())){
							if(finish.equals(sub_content.get(i).getFinish())){
								Content c = new Content(content.get(i).getTitle(),content.get(i).getId());
								BBSCarPoolContent s = new BBSCarPoolContent(sub_content.get(i).getNumber(), sub_content.get(i).getStart(), sub_content.get(i).getFinish(),
										sub_content.get(i).getTime(), sub_content.get(i).getContent(), sub_content.get(i).getDate(), sub_content.get(i).getLatitude(), sub_content.get(i).getLongitude());
								result_title.add(c);
								result_sub.add(s);
								cnt++;
							}
						}
					}
					Toast.makeText(BBSCarPoolTab2Search.this, cnt + "개의 검색결과", Toast.LENGTH_SHORT).show();
					
					Intent intent = new Intent(BBSCarPoolTab2Search.this, BBSCarPoolSearchResult2.class);
					Bundle b = new Bundle();
					b.putString("ID", id);
					intent.putExtras(b);
					b.putSerializable("TITLE", result_title);
					intent.putExtras(b);
					b.putSerializable("SUB", result_sub);
					intent.putExtras(b);
					startActivity(intent);
					cnt = 0;
				}
			}
		});

		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}

}
